package cn.zjyj.module.zjyj.controller.admin.tenantunit;

import cn.zjyj.framework.common.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.zjyj.framework.common.common.pojo.PageParam;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.framework.excel.core.util.ExcelUtils;

import cn.zjyj.framework.apilog.core.annotation.ApiAccessLog;

import static cn.zjyj.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.zjyj.module.zjyj.controller.admin.tenantunit.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.tenantunit.TenantUnitDO;
import cn.zjyj.module.zjyj.service.tenantunit.TenantUnitService;

@Tag(name = "管理后台 - 租户单元")
@RestController
@RequestMapping("/zjyj/tenant-unit")
@Validated
public class TenantUnitController {

    @Resource
    private TenantUnitService tenantUnitService;

    @PostMapping("/create")
    @Operation(summary = "创建租户单元")
    @PreAuthorize("@ss.hasPermission('zjyj:tenant-unit:create')")
    public CommonResult<Long> createTenantUnit(@Valid @RequestBody TenantUnitSaveReqVO createReqVO) {
        return success(tenantUnitService.createTenantUnit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户单元")
    @PreAuthorize("@ss.hasPermission('zjyj:tenant-unit:update')")
    public CommonResult<Boolean> updateTenantUnit(@Valid @RequestBody TenantUnitSaveReqVO updateReqVO) {
        tenantUnitService.updateTenantUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户单元")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:tenant-unit:delete')")
    public CommonResult<Boolean> deleteTenantUnit(@RequestParam("id") Long id) {
        tenantUnitService.deleteTenantUnit(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除租户单元")
    @PreAuthorize("@ss.hasPermission('zjyj:tenant-unit:delete')")
    public CommonResult<Boolean> deleteTenantUnitList(@RequestParam("ids") List<Long> ids) {
        tenantUnitService.deleteTenantUnitListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户单元")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:tenant-unit:query')")
    public CommonResult<TenantUnitRespVO> getTenantUnit(@RequestParam("id") Long id) {
        TenantUnitDO tenantUnit = tenantUnitService.getTenantUnit(id);
        return success(BeanUtils.toBean(tenantUnit, TenantUnitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户单元分页")
    @PreAuthorize("@ss.hasPermission('zjyj:tenant-unit:query')")
    public CommonResult<PageResult<TenantUnitRespVO>> getTenantUnitPage(@Valid TenantUnitPageReqVO pageReqVO) {
        PageResult<TenantUnitDO> pageResult = tenantUnitService.getTenantUnitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TenantUnitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户单元 Excel")
    @PreAuthorize("@ss.hasPermission('zjyj:tenant-unit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantUnitExcel(@Valid TenantUnitPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TenantUnitDO> list = tenantUnitService.getTenantUnitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "租户单元.xls", "数据", TenantUnitRespVO.class,
                BeanUtils.toBean(list, TenantUnitRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取单元精简信息列表", description = "只包含被开启的单元，主要用于前端的下拉选项")
    public CommonResult<List<TenantUnitSimpleRespVO>> getSimpleDeptList() {
        List<TenantUnitDO> list = tenantUnitService.getUnitList(
                new TenantUnitPageReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, TenantUnitSimpleRespVO.class));
    }

}